<?php
$this->breadcrumbs=array(
	'Comments',
);

$this->menu=array(
	array('label'=>'Create Comment', 'url'=>array('create')),
	array('label'=>'Manage Comment', 'url'=>array('admin')),
);
?>
<script type="text/javascript">
<!--


$(function(){
	
	$("#content").height($(".column1").height());

	$("#loading").remove();

});

function updateView(id){
	
	var url = '<?php echo $this->createUrl('itemview');?>';

	url = url.replace(/<?php echo Yii::app()->urlManager->urlSuffix;?>/,'/id/'+id+'<?php echo Yii::app()->urlManager->urlSuffix;?>');

	
//	alert(url);
//	$.get(url,function(msg){
//		alert(msg);
//	});
	
	$("#view-"+id).load(url);
}

function updateItem(id){
	var url = '<?php echo $this->createUrl('iteminfo');?>';

	url = url.replace(/<?php echo Yii::app()->urlManager->urlSuffix;?>/,'/id/'+id+'<?php echo Yii::app()->urlManager->urlSuffix;?>');

//	alert(url);
//	$.get(url,function(msg){
//		alert(msg);
//	});
	
	$("#reply-"+id).load(url);
}

function resourceHover(object){
		var id = object.attr("cid");

		$.get('<?php echo $this->createUrl('comment/view');?>',{'id':id},function(msg){

			$("#comment-detail").slideDown().html(msg);
//			$("#comment-detail").html("dfjsl"+id);
		});

		$(".view").removeClass("hoverBg");

		object.addClass("hoverBg");
		
}

function setDefaultContent(val){

	$(".reply-input").each(function(i){
		if($(this).val() == ''){
			$(this).val(val);

		}
//		if($(this).val() == val){
//			$(this).next().val('');
//		}
	});

}
function changeInputToTexteara(object){
//	object.parent().append('<textarea class="reply-input" style="width:90%;height:50px;" onblur="changeTextAeraToInput($(tihs))" />');
	object.hide().next().val(object.val()).show().next().show();
}

function changeTextAeraToInput(object){

	
	object.hide().prev().val(object.val()).show();
	object.next().hide();

	var id = object.attr("id");
	var ids = id.split("-");
	ids.pop();

	var params = ids.join(',');




//	id = ids[0];
	if(object.val() != '' && object.val() != '……'){

	    $.msgbox({
	        height:100,
	        width:250,
	        content:{type:'confirm', content: '回复完毕？'},
	        animation:4,
	        onClose:function(v){
				if(v){
					$.post('<?php echo $this->createUrl('comment/create'); ?>',{
							'params':params,
							'Comment[com_contents]':object.val(),
						},function(data){

							updateItem(id);

							//alert(data);
							
							if(data == 'ok'){
							       $.msgbox({
							            height:100,
							            width:250,
							            content:{type:'alert', content: '回复成功！'},
							            animation:4
							       });
				
							}
					});
				}
	        }
	   });
	}

//	var content = object.val();
//	alert(content);
//	object.parent().append('<input type="text" style="width:90%;padding:5px;background-color:white;" onfocus="changeInputToTexteara($(this))" class="box reply-input" />');
//	object.parent().find(".reply-input").val(content);
//	object.remove();
	
}

function actionCommentTrash(object){

	var url = object.attr("id");
	var id = object.attr("name");
	
    $.msgbox({
        height:100,
        width:250,
        content:{type:'alert', content: '确定将此评论放入垃圾箱？'},
        animation:4,
        onClose:function(v){
			$.post(url,{'Comment[com_status]':<?php echo Comment::COMMENT_TRASH;?>},function(data){
				if(data == 'ok'){
					$(".reply[id="+id+"]").css("background","yellow").slideUp();
				}		
							
			});			
        }
   });

}



function actionResourceIsComment(object){

	var id = object.attr("id");
	var ac = object.attr("name");

	//alert(id+'-'+ac);
	
	$.post('<?php echo $this->createUrl('comment/batchresource'); ?>',{'id':id,'ac':ac},function(data){
		//alert(data);

		updateView(id);

		if(data == 0){
		    $.msgbox({
		        height:100,
		        width:250,
		        content:{type:'alert', content: '修改成功！'},
		        animation:4,
		        onClose:function(){
					
		        }
		    });
		}	
	});

	
}

function actionResourceBatch(){

	
	var ids = getCheckedIds('resource-item');
	
	$.post('<?php echo $this->createUrl('comment/batchresource'); ?>',{'id':ids,'ac':$("#resource-action").val()},function(data){

		//alert(data);
		
		var arr = ids.split(",");

		for(i in arr){
			updateView(arr[i]);
		}
		
		if(data == 0){
		    $.msgbox({
		        height:100,
		        width:250,
		        content:{type:'alert', content: '修改成功！'},
		        animation:4
		    });
		}else{
		    $.msgbox({
		        height:100,
		        width:250,
		        content:{type:'alert', content: '有'+data+'个未成功进行！'},
		        animation:4
		    });		
		}
	});
}

function actionCommentsBatch(){
	var ids = getCheckedIds('comment-item');

	$.post('<?php echo $this->createUrl('comment/batchcomments'); ?>',{'id':ids,'ac':$("#comment-action").val()},function(data){
		
		var arr = ids.split(",");

		for(i in arr){
			updateItem(arr[i]);
		}
		
		if(data == 0){
		    $.msgbox({
		        height:100,
		        width:250,
		        content:{type:'alert', content: '修改成功！'},
		        animation:4
		    });
		}else{
		    $.msgbox({
		        height:100,
		        width:250,
		        content:{type:'alert', content: '有'+data+'个未成功进行！'},
		        animation:4
		    });		
		}
	});
}

function actionCommentLock(object){

	var url = object.attr("id");
	var id = object.attr("name");
	var sid = object.attr("sid");	

	json = {
		'Comment[com_status]':object.attr("sid")
	};

	var content = "确定要锁定此评论？";

	if(parseInt(sid) == <?php echo Comment::COMMENT_PUBLISHED;?>){
		content = "确定要解除锁定？";

	}
	
    $.msgbox({
        height:100,
        width:250,
        content:{type:'alert', content: content},
        animation:4,
        onClose:function(v){
			$.post(url, json, function(data){
				

				if(data == 'ok'){
					updateItem(id);
				}		
							
			});			
        }
   });
    
    
}

function load(){

	setDefaultContent('……');
	
	var ids = getIds();
	var id;
	var store = '';
	

	for(i in ids){
		
		id = ids[i];	

		
		
		$.get('<?php echo  $this->createUrl('comment/reply');?>',{id:id},function(data){
//			alert(data);
			if (data != 'false'){
				result = data.split('#-#');

				if($(".reply[cid="+result[0]+"]").find(".arc-content").length == 1){
					$(".reply[cid="+result[0]+"]").find(".arc-content").append(result[1]).fadeIn(5000);	
					
				}
			}
		});
		
	}
	
}

window.setInterval(load,10000);

function loadResourceList(){
	$("#resource-list").load('<?php echo $this->createUrl('comment/list');?>');
}

function getIds(){

	var ids = new Array();
	
	$(".reply").each(function(i){
		ids.push($(this).attr("cid"));
	});

	$("#djd").html(ids.join('-'));
	return ids;
}

function checkAll(object,name){
	if(object.attr("checked") == true){
		$("input[name="+name+"]").each(function(){
			$(this).attr("checked",true);
		});
	}else{
		$("input[name="+name+"]").each(function(){
			$(this).attr("checked",false);
		});
	}
	
}

function getCheckedIds(name){
	var ids = new Array();
	$("input[name="+name+"]").each(function(){
		if($(this).attr("checked") == true){
			ids.push($(this).val());
		}
	});
	

	return ids.join(',');
}
//-->
</script>
<div class="column1 colborder">
	<div class="box">
		<?php $this->beginWidget('zii.widgets.CPortlet', array('title'=>Yii::t('comment','Statistics')));?>
			
				<div class="box-content">
					<ul id="product-statistics">
						<li>共有<?php echo Comment::model()->count();?>条评论</li>
						<li>新闻评论有<?php echo Comment::model()->getStatisticsInfoByCategory('com_aid')?>条</li>
						<li>产品评论有<?php echo Comment::model()->getStatisticsInfoByCategory('com_mid');?>条</li>
						<li>下载评论有<?php echo Comment::model()->getStatisticsInfoByCategory('com_fid');?>条</li>
					</ul>
					<div style="text-align: right;"><a id="product-show" href="javascript:void();">显示所有分类</a></div>
				</div>
				
		<?php $this->endWidget();?>
	</div>
	<div class="box">
		<?php $this->beginWidget('zii.widgets.CPortlet', array('title'=>Yii::t('comment', 'Recent Comments')));?>			
			<div class="box-content">
				<span>
					<input type="checkbox" name="checkAll" onclick="checkAll($(this),'resource-item');" />
					<?php echo Yii::t('common','Check All');?>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<?php echo Yii::t('comment','Allow comments or not')?>：
					<?php echo CHtml::dropDownList($model,'',Article::model()->generateCommentStateDropdownList(),array(
						'id'=>'resource-action'
					));?>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="button" onclick="actionResourceBatch();" value="<?php echo Yii::t('admin','Submit');?>" />
				</span>	
				<hr class="space" />
				<div id="resource-list">
					<?php $this->widget('zii.widgets.CListView', array(
						'dataProvider'=>$dataProvider,
						'itemView'=>'_view',
					)); ?>				
				</div>

			</div>
				
		<?php $this->endWidget();?>
	</div>
</div>

<div class="column2" id="detail-info">
	<div class="box">
		<?php $this->beginWidget('zii.widgets.CPortlet', array('title'=>Yii::t('comment','Reply')));?>
			<div class="box-content">
				<div style="margin-bottom: 10px;">
					<input type="checkbox" name="checkAll" onclick="checkAll($(this),'comment-item');" value="<?php echo $data->com_id;?>" />
					<?php echo Yii::t('common','Check All');?>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<?php echo Yii::t('comment' ,'Comment State');?>：
					<?php echo CHtml::dropDownList($model,'',Comment::model()->generateCommentStateDropdownList(),array(
						'id'=>'comment-action'
					));?>
					&nbsp;&nbsp;&nbsp;&nbsp;
					<input type="button" onclick="actionCommentsBatch();" value="<?php echo Yii::t('admin','Submit');?>" />
					
				</div>	
				<div id="comment-detail">
				
				</div>			
			</div>
		<?php $this->endWidget();?>
	</div>

</div>

<?php $this->widget('ext.scrollfollow.scrollfollowWidget',array(
	'id'=>'#detail-info'
))?>
